package com.xugou.controller;

import com.xugou.pojo.User;
import com.xugou.service.Impl.UserServiceImpl;
import com.xugou.service.UserService;
import com.xugou.utils.Contants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    //跳转到登录界面
    @RequestMapping("/jumpLogin")
    public String jumpLogin(Model model){
        return "login";
    }

    //验证用户是否存在
    @RequestMapping(value = "/checkUsername",produces ="text/html; charset=UTF-8")
    @ResponseBody
    public String checkUsername(String name){
        String msg="";
        boolean flag = false;
        if (name!=null){
            flag = userService.checkUsername(name);
            if (flag) {
                msg = "用户名有效";
            }
            else {
                msg = "找不到用户名，请注册";
            }
        }else {
            msg = "没有用户名输入";
        }
        return msg;
    }

    //登录成功，跳转到后台
    @RequestMapping("/loginSuccess")
    public String login(HttpServletRequest req,Model model){

        String userName = req.getParameter("userName");
        String userPassword = req.getParameter("userPwd");

        //调用service层直接判断
        boolean b = userService.judgeLoginSuccess(userName, userPassword);
        if (b){
            //加入session
            req.getSession().setAttribute(Contants.USER_SEESION,userService.getUserByname(userName));
            User userByname = userService.getUserByname(userName);
            return "root_index";
        }else{
            return "false";
        }
    }

    //注销
    @RequestMapping("/logOut")
    public String logout(HttpSession session){
        //session过期
        session.invalidate();
        //跳转到登录页面
        return "login";
    }

    //跳转到修改界面
    @RequestMapping("/topwdModify")
    public String pwdModify(Model model){
        return "user/pwdModify";
    }

    //修改密码
    @RequestMapping("/pwdModify")
    public String pwdModify(HttpServletRequest req){
        User root= (User) req.getSession().getAttribute(Contants.USER_SEESION);
        System.out.println(root);
        System.out.println(root.getUserId()+" "+root.getUserName()+" "+root.getUserPwd());
        String oldpassword = req.getParameter("oldpassword");
        String newpassword = req.getParameter("newpassword");
        String rnewpassword = req.getParameter("rnewpassword");
        System.out.println(oldpassword);
        System.out.println(newpassword);
        System.out.println(rnewpassword);
        if(root.getUserPwd().equals(oldpassword)) {
            System.out.println("输入的旧密码正确");
            System.out.println(root.getUserPwd());
            System.out.println(root.getUserName());
            //旧密码输入正确
            if(newpassword != null && newpassword.length()!=0){
                if(newpassword.equals(rnewpassword)){
                    System.out.println("输入的两次密码一致");
                    root.setUserPwd(newpassword);
//                    int i = userService.updateUserInfo(newpassword,root.getUserId());
                    int i = userService.updateUserInfo(root);
                    req.setAttribute("message", "密码修改成功，请退出，使用新密码登录");
                    // 密码修改成功,移除session
                    System.out.println("密码修改成功，请退出，使用新密码登录");

                    req.getSession().removeAttribute(Contants.USER_SEESION);

                    System.out.println("重新登录");
                    //密码修改成功需要重新登录
                    return "redirect:/";
                }else{
                    System.out.println("新密码有问题");

                    //新密码有问题
                    req.setAttribute("message", "两次输入新密码不一致");
                    return "user/pwdmodify";
                }
            }else{
                System.out.println("新密码不能为空");
                //新密码不能为空
                req.setAttribute("message", "新密码不能为空");
                return "user/pwdmodify";
            }
        }else{
            System.out.println("旧密码输入不正确");
            System.out.println(root.getUserPwd());
            System.out.println(root.getUserName());
            //旧密码输入不正确
            req.setAttribute("message", "旧密码输入不正确");
            return "user/pwdmodify";
        }

    }

}
